package com.sijia.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sijia.reggie.comment.R;
import com.sijia.reggie.entity.Employee;
import com.sijia.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author 杨思佳
 * @version 1.0
 */

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService es;

    /**
     * 员工登录
     *
     * @param request
     * @param e
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee e) {
        String pwd = DigestUtils.md5DigestAsHex(e.getPassword().getBytes());
        LambdaQueryWrapper<Employee> elqw = new LambdaQueryWrapper<>();
        elqw.eq(Employee::getUsername, e.getUsername());
        Employee employee = es.getOne(elqw);
        if (employee == null) {
            return R.error("登陆失败，不存在该用户！！！");
        }
        if (!employee.getPassword().equals(pwd)) {
            return R.error("登陆失败，密码错误！！！");
        }
        if (employee.getStatus() == 0) {
            return R.error("登陆失败，账户处于冻结状态！！！");
        }
        //验证成功，将登录信息储存在session(一种服务端会话追踪技术)中
        request.getSession().setAttribute("employee", employee.getId());
        return R.success(employee);
    }

    /**
     * 新增员工
     *
     * @param request
     * @param e
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee e) {
//        Long createId = (Long) request.getSession().getAttribute("employee");
//        e.setCreateTime(LocalDateTime.now());
//        e.setCreateUser(createId);
//        e.setUpdateTime(LocalDateTime.now());
//        e.setUpdateUser(createId);
        e.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        es.save(e);
        return R.success("添加成功");
    }

    /**
     * 注销登录
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageFind(int page, int pageSize, String name) {
        //构造分页构造器
        Page p = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        if (name != null) {
            queryWrapper.like(Employee::getName, name);
        }
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        es.page(p, queryWrapper);
        return R.success(p);
    }


    /**
     * 根据id，修改信息
     *
     * @return
     */
    @PutMapping
    public R<String> updateInfo(HttpServletRequest request, @RequestBody Employee e) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        Long id = (Long) request.getSession().getAttribute("employee");
//        e.setUpdateTime(LocalDateTime.now());
//        e.setUpdateUser(id);
        es.updateById(e);
        return R.success("员工信息更新成功");
    }


    /**
     * 根据id查询员工信息
     *
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId, id);
        Employee employee = es.getOne(queryWrapper);
        if (employee != null) {
            return R.success(employee);
        } else {
            return R.error("查询失败");
        }
    }
}
